package com.ttbj.controller.work.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyun.oss.common.utils.HttpUtil;
import com.ttbj.constant.order.ExtraTypeEnum;
import com.ttbj.controller.work.WorkController;
import com.ttbj.core.util.CurrencyUtils;
import com.ttbj.product.ConvertPredict;
import com.ttbj.product.api.AppletUserFacade;
import com.ttbj.product.api.CostFacade;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.OriginalPayFacade;
import com.ttbj.product.api.PayFacade;
import com.ttbj.product.api.WorkOrderFacade;
import com.ttbj.product.bean.dto.BillCostDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.OwnerDto;
import com.ttbj.product.bean.dto.PageGlobal;
import com.ttbj.product.bean.dto.QRCodeOrderDto;
import com.ttbj.product.bean.dto.SalesmanDto;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.product.bean.dto.WorkListDto;
import com.ttbj.util.Assert;
import com.ttbj.util.RequestUtils;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/salesman")
public class WorkControllerImpl implements WorkController {
	private static final Logger logger = LoggerFactory.getLogger(WorkControllerImpl.class);

	@Autowired
	private WorkOrderFacade workOrderFacade = null;

	@Autowired
	private CostFacade costFacade = null;

	@Autowired
	private OrderFacade orderFacade = null;

	@Autowired
	private OriginalPayFacade originalPayFacade = null;

	@RequestMapping(value = "/get/order/all.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData getWorkOrderAll(HttpServletRequest request, String token) throws Exception {
		SalesmanDto salesman = (SalesmanDto) request.getAttribute("user");
		Assert.assertNonNullField(salesman, "业务员信息缺失", 502, null, "salesmanId");
		List<WorkListDto> list = workOrderFacade.getWorkList(salesman.getSalesmanId());
		return ViewModelUtil.buildSuccessPage("成功", list, list.size());
	}

	@RequestMapping(value = "/get/order/list.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData getWorkList(HttpServletRequest request, String token, Integer isCompl, PageGlobal page) {
		SalesmanDto salesman = (SalesmanDto) request.getAttribute("user");
		List<WorkListDto> workList = workOrderFacade.getWorkList(salesman.getSalesmanId(), isCompl, page);
		return ViewModelUtil.buildSuccessPage("成功", workList, workList.size());
	}

	@RequestMapping(value = "/detail/order.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData getWorkOrderDetail(HttpServletRequest request, String token, Long orderId) throws Exception {
		OrderDto dto = workOrderFacade.getWorkOrderDetail(orderId); // 如果确认为需要支付,orderPayState为0
		return ViewModelUtil.buildSuccess("成功", dto);
	}

	/**
	 * 业务员工单处理，上门确认接口 此接口用于上门勘察。 返回支付二维码图片url
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/confir/order.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData workConfir(HttpServletRequest request, //request
			@RequestParam("workId") Long workId, // workId
			@RequestParam("token") String token, // token
			@RequestParam(value = "floorNum", required = false) Integer floorNum, // 楼层
			@RequestParam(value = "pieceNum", required = false) Integer pieceNum, // 件数
			@RequestParam(value = "baleNum", required = false) Integer baleNum, // 打包数
			@RequestParam(value = "truckType", required = false) String truckType, // 车辆类型
			@RequestParam(value = "serciveLevel", required = false) String serciveType, // 服务类型
			@RequestParam(value = "toCarry", required = false) Integer orderToCarry, // 是否搬运
			@RequestParam(value = "toPack", required = false) Integer orderToPack, // 是否打包
			@RequestParam(value = "orderDriverRequire", required = false) Integer orderDriverRequire, // 车辆数
			@RequestParam(value = "fee", required = false) Float fee, //  用户预估费用
			@RequestParam(value = "carryFee", required = false) Float carryFee, // 用户自定义搬运费用
			@RequestParam(value = "packFee", required = false) Float packFee// 用户自定义打包费用
	) throws Exception {
		// 获取带有搬入，搬出地信息的订单信息
		Long orderId = workOrderFacade.getWorkOrder(workId);
		orderFacade.isPay(String.valueOf(orderId));
		// <-优惠券暂时没有->
		// 计算搬家费
		BillCostDto costDto = costFacade.totalMoveCost(fee, carryFee, packFee, truckType, serciveType, orderDriverRequire);
		Float allFee = costDto.getAllFee();
		// 修改可支付状态和各项费用以及是否搬家是否打包状态
		workOrderFacade.confirm(workId, orderId, allFee, fee, carryFee, packFee, orderToCarry, orderToPack, truckType, orderDriverRequire, floorNum, baleNum);
		// 支付二维码
		Map<String, String> appPay = originalPayFacade.appPay(getXmlPath(), "pay/", RequestUtils.getRequestPath(request), allFee, null, String.valueOf(orderId));
		Assert.assertNonNullObj(appPay, 232, "支付参数失败");
		appPay.put("carriage", String.valueOf(costDto.getCarriage()));
		appPay.put("carryFee", String.valueOf(carryFee));
		appPay.put("packFee", String.valueOf(packFee));
		appPay.put("allFee", String.valueOf(allFee));
		return ViewModelUtil.buildSuccess("成功", appPay);

	};
	
	
	/**
	 * 估算搬家费用
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/move/estim.do")
	@ResponseBody
	public ViewData moveCostEstim(HttpServletRequest request, //request
			@RequestParam("workId") Long workId, // workId
			@RequestParam("token") String token, // token
			@RequestParam(value = "floorNum", required = false) Integer floorNum, // 楼层
			@RequestParam(value = "pieceNum", required = false) Integer pieceNum, // 件数
			@RequestParam(value = "baleNum", required = false) Integer baleNum, // 打包数
			@RequestParam(value = "truckType", required = false) String truckType, // 车辆类型
			@RequestParam(value = "serciveLevel", required = false) String serciveType, // 服务类型
			@RequestParam(value = "toCarry", required = false) Integer orderToCarry, // 是否搬运
			@RequestParam(value = "toPack", required = false) Integer orderToPack, // 是否打包
			@RequestParam(value = "orderDriverRequire", required = false) Integer orderDriverRequire, // 车辆数
			@RequestParam(value = "carryFee", required = false) Float carryFee, // 用户自定义搬运费用
			@RequestParam(value = "packFee", required = false) Float packFee// 用户自定义打包费用
	) throws Exception {
		// 获取带有搬入，搬出地信息的订单信息
		BillCostDto costDto = costFacade.totalMoveCost( carryFee, packFee, truckType, serciveType, orderDriverRequire);
		return ViewModelUtil.buildSuccess("成功", costDto);
	}
	
	

	@RequestMapping(value = "/pay/transf.do", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData payTransf(HttpServletRequest request, String token, Long orderId, Long workId) throws Exception {
		// 获取带有搬入，搬出地信息的订单信息
		orderId = workOrderFacade.getWorkOrder(workId);
		Assert.assertNonNullObj(orderId, 234);// 非空判断
		OrderDto queryOrder = orderFacade.queryOrder(orderId);
		orderFacade.isPay(String.valueOf(orderId));
		Map<String, String> appPay = originalPayFacade.appPay(getXmlPath(), "pay/", RequestUtils.getRequestPath(request), queryOrder.getOrderAllPay(), null, String.valueOf(queryOrder.getOrderId()));
		Assert.assertNonNullObj(appPay, 232, "获取支付信息map为null,map");
		appPay.put("carriage", String.valueOf(queryOrder.getOrderCarriage()));
		appPay.put("carryFee", String.valueOf(queryOrder.getOrderMovePay()));
		appPay.put("packFee", String.valueOf(queryOrder.getOrderPackPay()));
		appPay.put("allFee", String.valueOf(queryOrder.getOrderAllPay()));
		return ViewModelUtil.buildSuccess("成功", appPay);
	}

	@RequestMapping(value = "/get/order/qr", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public ViewData getOrderQR(HttpServletRequest request, String token, Long orderId) throws Exception {
		QRCodeOrderDto orderCode = orderFacade.getOrderCode(orderId);
		return ViewModelUtil.buildSuccess("成功", orderCode);
	}
	
	
	
	
	/**
	 * 获取根路径
	 * 
	 * @return
	 */
	public String getXmlPath() {
		String classpath = this.getClass().getResource("/").getPath();
		String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");
		return webappRoot;
	}

}
